package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyTagMapper;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.article.vo.StrategyDataPersistence;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Autowired
    private StrategyContentMapper strategyContentMapper;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyTagService strategyTagService;

    @Autowired
    private StrategyTagMapper tagMapper;

    @Override
    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyDetailMapper.selectForList(qo));
    }
    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    public void saveOrUpdate(StrategyDetail detail, String tags) {
        //设置目的地
        Strategy strategy = strategyService.get(detail.getStrategy().getId());
        detail.setDest(strategy.getDest());

        //设置作者,用到参数解析器

        //设置摘要
        String content = detail.getStrategyContent().getContent();
        if (content.length() >200){
            detail.setSummary(content.substring(0,200));
        }else {
            detail.setSummary(content);
        }

        //是否国内
        List<Destination> toasts = destinationService.getToasts(strategy.getDest().getId());
        detail.setIsabroad(toasts.get(0).getId() != 1);

        if (detail.getId() == null){
            //设置创建时间
            detail.setCreateTime(new Date());
            //为空,新增操作
            strategyDetailMapper.insert(detail);
            //添加内容
            StrategyContent strategyContent = detail.getStrategyContent();
            strategyContent.setId(detail.getId());
            strategyContentMapper.insert(strategyContent);

        }else {
            //编辑操作
            strategyDetailMapper.updateByPrimaryKey(detail);

            //编辑内容
            StrategyContent strategyContent = detail.getStrategyContent();
            strategyContent.setId(detail.getId());
            strategyContentMapper.updateByPrimaryKey(strategyContent);
        }

        //处理标签
        if (StringUtils.hasLength(tags)){
            //先删除旧关系,再保存新关系
            strategyDetailMapper.deleteRelation(detail.getId());
            String[] ts = tags.split(",");
            if (ts.length>0){
                for (String tagName: ts) {
                    StrategyTag tag = tagMapper.getName(tagName);
                    //判断传入的标签数据库是否存在
                    if (tag == null){
                        tag = new StrategyTag();
                        tag.setName(tagName);
                        //保存
                        tagMapper.insert(tag);
                    }
                    //维护文章和标签的关系
                    strategyDetailMapper.insertRelation(tag.getId(),detail.getId());
                }
            }

        }
    }

    public StrategyContent getContent(Long id) {

        return strategyContentMapper.selectByPrimaryKey(id);
    }

    public List<StrategyDetail> listViewnumTop3(Long id) {

        return strategyDetailMapper.selectViewnumTop3(id);
    }

    public void updateStatisVO(StrategyDataPersistence pvo) {

        strategyDetailMapper.updateStatisVO(pvo);
    }
}
